﻿using Dapper.Contrib.Extensions;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Tauren.Business.Entity.Global;
using Tauren.Business.Entity.Model;
using Tauren.Business.Entity.Output;
using Dapper;

namespace Tauren.Business.DLL.System_Log
{
    /// <summary>
    /// 日志操作
    /// </summary>
    public class SystemLogBusiness : ISystemLogBusiness
    {
        public SystemLogBusiness(IDbConnection dbConnection)
        {
            this.dbConnection = dbConnection;
        }

        public IDbConnection dbConnection { get; set; }
        /// <summary>
        /// 新增租户
        /// </summary>
        /// <param name="tenant"></param>
        /// <returns></returns>
        public long Add(Entity.Model.SystemLog model) => dbConnection.Insert<Entity.Model.SystemLog>(model);

        /// <summary>
        /// 获取所有的租户信息
        /// </summary>
        /// <returns></returns>
        public List<Entity.Model.SystemLog> All() => dbConnection.GetAll<Entity.Model.SystemLog>().ToList();

        public SystemLog GetLog(long id) => dbConnection.Get<Entity.Model.SystemLog>(id);

        /// <summary>
        /// 分页获取日志信息
        /// </summary>
        /// <param name="queryModel"></param>
        /// <param name="Total"></param>
        /// <returns></returns>
        public List<SystemLogTable> Pages(QueryModel queryModel, out long Total)
        {
            Total = 0;
            string where = "";
            if (!string.IsNullOrEmpty(queryModel.KeyWord))
            {
                where += $"  where (s.account like '%{queryModel.KeyWord}%' or s.tcnname like '%{queryModel.KeyWord}%' or s.code like '%{queryModel.KeyWord}%' or s.empcode like '%{queryModel.KeyWord}%' or s.enname like '%{queryModel.KeyWord}%' or s.cnname like '%{queryModel.KeyWord}%' )";
            }
            if (string.IsNullOrEmpty(queryModel.SortField))
            {
                queryModel.SortField = "s.AutoId";
                queryModel.IsAsc = false;
            }
            else
            {
                queryModel.SortField = queryModel.SortField.ToLower().Replace("dic_", "").Replace("dic", "").Trim();
            }
            if (queryModel.paramters.Any())
            {
                if (string.IsNullOrEmpty(queryModel.KeyWord)) where = " where  "; else where += " AND ";
                where = where  + string.Join(" AND ", queryModel.paramters.Select(o => $"s.{o.Label}  like '%{o.Value}%'"));
            }
            string asc = queryModel.IsAsc ? "ASC" : "DESC";
            queryModel.Page = queryModel.Page == 0 ? 1 : queryModel.Page;
            StringBuilder sql = new StringBuilder();
            sql.Append(@$"select count(s.autoid) from systemlog s {where} ;");
            sql.Append(@$"select s.autoid,s.account,s.cnName,s.enName,s.empCode,s.tCnname,s.code,s.phone,s.interfacePath,s.ipAddress,s.serverAddress,s.language from systemlog s  {where}  ORDER BY {queryModel.SortField} {asc} LIMIT {(queryModel.Page - 1) * queryModel.PageSize},{ queryModel.PageSize} ;");
            using (var reader = dbConnection.QueryMultiple(sql.ToString()))
            {
                Total = reader.ReadFirstOrDefault<int>();
                List<SystemLogTable> pageModules = reader.Read<SystemLogTable>().ToList();
                long RowNo = ((queryModel.Page == 0 ? 1 : queryModel.Page) - 1) * queryModel.PageSize + 1;
                pageModules.ForEach(o =>
                {
                    o.RowNo = RowNo;
                    RowNo++;
                });
                return pageModules;
            }
        }
    }
}
